package com.mf.ergate.controller.cust;

import com.mf.ergate.api.cust.CustomerInfoRemoteService;
import com.mf.ergate.api.cust.model.dto.CustomerInfoCreateDto;
import com.mf.ergate.api.cust.model.dto.CustomerInfoDeleteDto;
import com.mf.ergate.api.cust.model.dto.CustomerInfoQueryDto;
import com.mf.ergate.api.cust.model.dto.CustomerInfoUpdateDto;
import com.mf.ergate.api.cust.model.vo.CustomerInfoVo;
import com.mf.ergate.common.utils.MfBeanUtils;
import com.mf.ergate.domain.model.cust.CustomerInfoEntity;
import com.mf.ergate.service.cust.CustomerInfoService;
import com.mf.framework.common.model.Page;
import com.mf.framework.common.model.RemotePage;
import com.mf.framework.common.model.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 客户信息Controller
 * User: zhaoming
 * DateTime: 2019-01-27
 * To change this template use File | Settings | File Templates.
 */
@RestController
public class CustomerInfoController implements CustomerInfoRemoteService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private CustomerInfoService customerInfoService;

    /**
     * 新增
     * @param customerInfoCreateDto 客户信息新增Dto
     * @return 客户信息
     */
    public Result<CustomerInfoVo> insert(@RequestBody CustomerInfoCreateDto customerInfoCreateDto) {
        CustomerInfoEntity customerInfoEntity = MfBeanUtils.copyObject(customerInfoCreateDto, CustomerInfoEntity.class);
        customerInfoEntity = customerInfoService.insert(customerInfoEntity);
        CustomerInfoVo customerInfoVo = MfBeanUtils.copyObject(customerInfoEntity, CustomerInfoVo.class);
        return new Result<>(customerInfoVo);
    }

    /**
     * 根据唯一编号更新
     * @param customerInfoUpdateDto 客户信息更新Dto
     * @return 执行结果
     */
    public Result<Boolean> updateByUniqueNo(@RequestBody CustomerInfoUpdateDto customerInfoUpdateDto) {
        CustomerInfoEntity customerInfoEntity = MfBeanUtils.copyObject(customerInfoUpdateDto, CustomerInfoEntity.class);
        Boolean flag = customerInfoService.updateByUniqueNo(customerInfoEntity);
        return new Result<>(flag);
    }

    /**
     * 根据唯一编号删除(逻辑删除)
     * @param customerInfoDeleteDto 客户信息删除Dto
     * @return 结果集
     */
    public Result<Boolean> deleteByUniqueNo(@RequestBody CustomerInfoDeleteDto customerInfoDeleteDto){
        CustomerInfoEntity customerInfoEntity = MfBeanUtils.copyObject(customerInfoDeleteDto, CustomerInfoEntity.class);
        Boolean flag = customerInfoService.deleteByUniqueNo(customerInfoEntity);
        return new Result<>(flag);
    }

    /**
     * 根据唯一编号查询
     * @param uniqueNo 唯一编号
     * @return 客户信息
     */
    public Result<CustomerInfoVo> selectByUniqueNo(@RequestBody String uniqueNo) {
        CustomerInfoEntity customerInfoEntity = customerInfoService.selectByUniqueNo(uniqueNo);
        CustomerInfoVo customerInfoVo = MfBeanUtils.copyObject(customerInfoEntity, CustomerInfoVo.class);
        return new Result<>(customerInfoVo);
    }

    /**
     * 根据条件查询
     * @param customerInfoQueryDto 客户信息查询Dto
     * @return 客户信息列表
     */
    public Result<List<CustomerInfoVo>> selectByCondition(@RequestBody CustomerInfoQueryDto customerInfoQueryDto) {
        List<CustomerInfoEntity> customerInfoEntities = customerInfoService.selectByCondition(customerInfoQueryDto);
        List<CustomerInfoVo> customerInfoVos = MfBeanUtils.copyList(customerInfoEntities, CustomerInfoVo.class);
        return new Result<>(customerInfoVos);
    }

    /**
     * 根据条件查询数量
     * @param customerInfoQueryDto 客户信息查询Dto
     * @return 数量
     */
    public Result<Integer> selectCount(@RequestBody CustomerInfoQueryDto customerInfoQueryDto) {
        return new Result<>(customerInfoService.selectCount(customerInfoQueryDto));
    }

    /**
     * 根据条件分页查询
     * @param remotePage 远程分页对象
     * @return 客户信息分页列表
     */
    public Result<Page<CustomerInfoVo>> selectPage(@RequestBody RemotePage<CustomerInfoQueryDto> remotePage){
        Page<CustomerInfoEntity> customerInfoEntityPage = customerInfoService.selectPage(remotePage.getPage(), remotePage.getObj());
        Page<CustomerInfoVo> page = MfBeanUtils.copyPage(customerInfoEntityPage, CustomerInfoVo.class);
        return new Result<>(page);
    }

}